<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>git 进阶操作命令 | Finen&#39;s Blog</title>
    <meta name="generator" content="VuePress 1.4.1">
    <link rel="icon" href="/logo.png">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/icons/apple-touch-icon-152x152.png">
    <link rel="mask-icon" href="/icons/safari-pinned-tab.svg" color="#3eaf7c">
    <meta name="description" content="Stay Hungry! Stay Foolish!">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.331bc503.css" as="style"><link rel="preload" href="/assets/js/app.8657dc36.js" as="script"><link rel="preload" href="/assets/js/3.655fda8f.js" as="script"><link rel="preload" href="/assets/js/59.d3045373.js" as="script"><link rel="preload" href="/assets/js/11.32858d9c.js" as="script"><link rel="prefetch" href="/assets/js/1.af5e15a9.js"><link rel="prefetch" href="/assets/js/10.3d57a2e0.js"><link rel="prefetch" href="/assets/js/12.cef9d203.js"><link rel="prefetch" href="/assets/js/13.02beb1f0.js"><link rel="prefetch" href="/assets/js/14.7efc03c9.js"><link rel="prefetch" href="/assets/js/15.6bbbab3f.js"><link rel="prefetch" href="/assets/js/16.f90edb71.js"><link rel="prefetch" href="/assets/js/17.0d4d6c13.js"><link rel="prefetch" href="/assets/js/18.76dd8979.js"><link rel="prefetch" href="/assets/js/19.7da82e6f.js"><link rel="prefetch" href="/assets/js/20.55ee1642.js"><link rel="prefetch" href="/assets/js/21.088a02d1.js"><link rel="prefetch" href="/assets/js/22.73d5a697.js"><link rel="prefetch" href="/assets/js/23.829e24c3.js"><link rel="prefetch" href="/assets/js/24.2f258433.js"><link rel="prefetch" href="/assets/js/25.0a52df22.js"><link rel="prefetch" href="/assets/js/26.1c66cc2f.js"><link rel="prefetch" href="/assets/js/27.aa781144.js"><link rel="prefetch" href="/assets/js/28.b2cf7947.js"><link rel="prefetch" href="/assets/js/29.b61b1598.js"><link rel="prefetch" href="/assets/js/30.ba0ae8b1.js"><link rel="prefetch" href="/assets/js/31.7637d043.js"><link rel="prefetch" href="/assets/js/32.e552a016.js"><link rel="prefetch" href="/assets/js/33.334376b0.js"><link rel="prefetch" href="/assets/js/34.08c95f99.js"><link rel="prefetch" href="/assets/js/35.76b6b95d.js"><link rel="prefetch" href="/assets/js/36.539b6d80.js"><link rel="prefetch" href="/assets/js/37.850783a5.js"><link rel="prefetch" href="/assets/js/38.06c33708.js"><link rel="prefetch" href="/assets/js/39.85fc61d5.js"><link rel="prefetch" href="/assets/js/4.12c21fd1.js"><link rel="prefetch" href="/assets/js/40.cdc93796.js"><link rel="prefetch" href="/assets/js/41.be79357e.js"><link rel="prefetch" href="/assets/js/42.e1ea76e5.js"><link rel="prefetch" href="/assets/js/43.06bf748b.js"><link rel="prefetch" href="/assets/js/44.ebd514b5.js"><link rel="prefetch" href="/assets/js/45.12908515.js"><link rel="prefetch" href="/assets/js/46.78fb0130.js"><link rel="prefetch" href="/assets/js/47.1a3b6cfd.js"><link rel="prefetch" href="/assets/js/48.ef04cdb5.js"><link rel="prefetch" href="/assets/js/49.763fd944.js"><link rel="prefetch" href="/assets/js/5.e50a6b6f.js"><link rel="prefetch" href="/assets/js/50.c8a7a24d.js"><link rel="prefetch" href="/assets/js/51.aaa9e6c7.js"><link rel="prefetch" href="/assets/js/52.7d3a85df.js"><link rel="prefetch" href="/assets/js/53.00784a7d.js"><link rel="prefetch" href="/assets/js/54.bd3845dd.js"><link rel="prefetch" href="/assets/js/55.10a67e4b.js"><link rel="prefetch" href="/assets/js/56.eb909b5a.js"><link rel="prefetch" href="/assets/js/57.4b821666.js"><link rel="prefetch" href="/assets/js/58.1db8af1f.js"><link rel="prefetch" href="/assets/js/6.f25d4964.js"><link rel="prefetch" href="/assets/js/60.e1ccc619.js"><link rel="prefetch" href="/assets/js/61.eca4de24.js"><link rel="prefetch" href="/assets/js/62.be7238a5.js"><link rel="prefetch" href="/assets/js/63.a6ace044.js"><link rel="prefetch" href="/assets/js/64.bbd88e61.js"><link rel="prefetch" href="/assets/js/65.1f6c1848.js"><link rel="prefetch" href="/assets/js/66.d22e96e8.js"><link rel="prefetch" href="/assets/js/67.80ad6427.js"><link rel="prefetch" href="/assets/js/68.130aa1f6.js"><link rel="prefetch" href="/assets/js/69.4b778299.js"><link rel="prefetch" href="/assets/js/7.9c54e368.js"><link rel="prefetch" href="/assets/js/70.edcac904.js"><link rel="prefetch" href="/assets/js/71.90aa842c.js"><link rel="prefetch" href="/assets/js/72.4cb8aeae.js"><link rel="prefetch" href="/assets/js/73.6f272f7b.js"><link rel="prefetch" href="/assets/js/74.d36a7a16.js"><link rel="prefetch" href="/assets/js/75.362400fa.js"><link rel="prefetch" href="/assets/js/76.140673a6.js"><link rel="prefetch" href="/assets/js/77.5f1336bf.js"><link rel="prefetch" href="/assets/js/78.6105e050.js"><link rel="prefetch" href="/assets/js/79.dd680c04.js"><link rel="prefetch" href="/assets/js/8.786d8198.js"><link rel="prefetch" href="/assets/js/80.ebfb944f.js"><link rel="prefetch" href="/assets/js/81.892d344c.js"><link rel="prefetch" href="/assets/js/9.b0b77105.js">
    <link rel="stylesheet" href="/assets/css/0.styles.331bc503.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Finen's Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/post/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/archives/" class="nav-link">
  归档
</a></div><div class="nav-item"><a href="/tags/" class="nav-link">
  标签云
</a></div><div class="nav-item"><a href="/friends/" class="nav-link">
  友人帐
</a></div> <a href="https://github.com/hirCodd" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/post/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/archives/" class="nav-link">
  归档
</a></div><div class="nav-item"><a href="/tags/" class="nav-link">
  标签云
</a></div><div class="nav-item"><a href="/friends/" class="nav-link">
  友人帐
</a></div> <a href="https://github.com/hirCodd" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <!----> </aside> <main class="page"> <div class="title-content"><div class="el-card box-cards is-never-shadow"><!----><div class="el-card__body"><h2>git 进阶操作命令</h2> <div class="page-info"><i aria-hidden="true" class="el-icon-s-custom"></i> <span class="i-text">Finen</span> <i aria-hidden="true" class="el-icon-collection-tag"><span class="i-text">git</span></i> <i aria-hidden="true" class="el-icon-time"></i> <span class="i-text">2018-03-28</span> <span id="/blog/others/git/git-advanced-command.html" data-flag-title="git 进阶操作命令" class="leancloud-visitors"><i aria-hidden="true" class="el-icon-view"></i> <i class="leancloud-visitors-count">1000000</i></span></div></div></div></div> <div class="theme-default-content content__default"><h1 id="工作区与暂存区"><a href="#工作区与暂存区" class="header-anchor">#</a> 工作区与暂存区</h1> <h2 id="工作区"><a href="#工作区" class="header-anchor">#</a> 工作区</h2> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>工作区就是在你电脑上的Repository下的某一个工作目录，比如：F:\Repository\test01这个目录。test01这个目录就是工作区，或者你新建的任何一个目录，都是工作区（.git隐藏目录下并非工作区）</p></div> <h2 id="暂存区"><a href="#暂存区" class="header-anchor">#</a> 暂存区</h2> <p>Repository目录下有一个（.git隐藏目录），这个就是控制版本的版本控制库。</p> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>（.git隐藏目录）存储了Git为我们创建的各种信息，包括版本信息，暂存区（这是最重要的），还有分支信息（Git已经为我们创建了一个master分支），还有一些配置信息等。</p></div> <p>接下来在Repository目录下新建一个test.txt文件，在里面写入123456，然后进行下面的这些操作。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_add_commit3.png" alt="git_add_commit3"></p> <p>继续在test.txt中添加一行数据，然后使用git status查看信息。git会提示你有文件需要git add 并且git commit。</p> <p>继续下面的操作！---把同一个文件提交到同一个暂存区中，并且使用commit提交到一个分支中。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_status_add_commit.png" alt="git_status_add_commit"></p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_status_commit.png" alt="git_status_commit"></p> <p>这次将所有文件提交到一个分支（master分支）中了，可以使用git log查看你提交的所有分支。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_log4.png" alt="git_log4"></p> <p>这样看起来很碍眼，而且版本较多的话，看起来的确不方便，所有推荐使用</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git log --pretty=oneline
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>接下来，你可以使用<a href="https://finen.top/git-basic-command/#more" target="_blank" rel="noopener noreferrer">上一篇git基础教程<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>讲过的方法对版本进行操作。基本命令请点击链接去查看...</p> <h1 id="撤销修改与删除文件操作"><a href="#撤销修改与删除文件操作" class="header-anchor">#</a> 撤销修改与删除文件操作</h1> <h2 id="撤销修改"><a href="#撤销修改" class="header-anchor">#</a> 撤销修改</h2> <h3 id="最简单的方法：手动修改"><a href="#最简单的方法：手动修改" class="header-anchor">#</a> 最简单的方法：手动修改</h3> <p>在你记得你修改了那些地方的情况下，直接手动撤销那些你不要的东西即可。</p> <h3 id="使用之前所使用的命令进行版本回滚"><a href="#使用之前所使用的命令进行版本回滚" class="header-anchor">#</a> 使用之前所使用的命令进行版本回滚</h3> <p>具体命令如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git log --pretty=oneline

git reset --hard HEAD^

git reset --hard HEAD~*

git reflog

git reset --hard 版本号
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>你可以使用这些办法进行你所需要的回滚。</p> <h3 id="使用git-checkout-test-txt-一点要带文件名，不然会创建分支！"><a href="#使用git-checkout-test-txt-一点要带文件名，不然会创建分支！" class="header-anchor">#</a> 使用git checkout -- test.txt <font color="red">一点要带文件名</font>，不然会创建分支！</h3> <p>git checkout --test.txt 分为两种状态</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout --test.txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><blockquote><ol><li>test.txt文件修改后，你没有使用git add命令对其进行提交到暂存区。使用git checkout --test.txt会回退到之前的一个版本。<strong>相当于回滚到你最后提交的那个版本。而刚刚修改的内容也将全部丢失。</strong></li></ol></blockquote> <blockquote><ol start="2"><li>另一种情况就是你已经使用git add提交文件到暂存区了。那这样使用git checkout --test.txt 就会<strong>保存已经在暂存区的内容。而没有写入暂存区的内容则会全部丢失。</strong></li></ol></blockquote> <p>接下来使用git进行操作查看。</p> <p>首先在test.txt文件中添加一行数据 654321，然后使用git add命令提交到暂存区。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_add2.png" alt="git_add2"></p> <p>接着再在test.txt文件中添加一行数据 222222，然后我们不使用git add命令提交文件到暂存区。</p> <p>最后我们使用git checkout --test.txt命令撤销修改，在查看文件。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_checkout.png" alt="git_checkout"></p> <p>跟上面的结论是一样的。</p> <h2 id="删除文件"><a href="#删除文件" class="header-anchor">#</a> 删除文件</h2> <h3 id="rm-命令（linux下的软删除命令）"><a href="#rm-命令（linux下的软删除命令）" class="header-anchor">#</a> rm 命令（linux下的软删除命令）</h3> <p>假如有人提交了tesxt02.txt的文件，但是这个文件缺失不需要的，我们可以使用rm test02.txt，然后在git commit就可以了。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>新建一个test02.txt 文件
git add test02.txt
git commit -m &quot;提交test02文件&quot;
rm test02.txt
git commit -m &quot;删除test02文件&quot;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_add_commit4.png" alt="git_commit4"></p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/rm.png" alt="rm"></p> <p>说明已经删除了文件并且已经commit了，当然假如你删除错误的话，你也可以回滚版本。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_reflog2.png" alt="git_reflog"></p> <p>假如你已经使用rm test02.txt命令，但是却没有使用git commit命令呢？又改如何恢复文件？你可以使用下面的命令:</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout --test02.txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>rm之前：</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_rm.png" alt="rm_before"></p> <p>rm 之后：</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_rm2.png" alt="git_rm_after"></p> <p>然后我们不使用git commit命令，使用git checkout -- test02.txt</p> <p>使用git checkout -- test02.txt之后</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_rm_after.png" alt="git_check_out"></p> <p>文件又恢复了，恢复多个文件也是同理。</p> <hr> <div class="language- extra-class"><pre><code> 下面会涉及删除与撤销的操作!请继续浏览！
</code></pre></div><hr></div> <footer class="page-edit"><!----> <!----></footer> <!---->  <div class="vcomment"><div id="vcomments"></div></div></main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.8657dc36.js" defer></script><script src="/assets/js/3.655fda8f.js" defer></script><script src="/assets/js/59.d3045373.js" defer></script><script src="/assets/js/11.32858d9c.js" defer></script>
  </body>
</html>
